package cs.ui250.carnatic;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class FlashCardsActivity extends Activity {

	private final static int RAGA_FROM_FLASH_CARDS = 0;
	private final static int NUM_RAGAS = 72;
	private final static String PROMPTS[] = {
			"Identify the raga by this audio clip.",
			"Identify the raga comprised of the following swaras.",
			"Identify the swaras that make up the following raga." };

	private FlashCardsActivity mInstance = this;
	private Context mContext;
	private DBAdapter mDB;
	private SoundManager mSoundManager;
	private Random mRandom;

	private Raga mRaga;
	private int mPromptType;

	private Button mGoToRagaButton;

	private TextView mPromptText;
	private TextView mQuestionText;
	private TextView mAnswerText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flashcard);
		Button mAnswerButton = (Button) findViewById(R.id.flashcard_show_answer_button);
		mAnswerButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showAnswer();
			}
		});
		mGoToRagaButton = (Button) findViewById(R.id.flashcard_go_to_raga_button);
		mGoToRagaButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(FlashCardsActivity.this,
						RagaDisplay.class);
				intent.putExtra("RAGA_ID_KEY", mRaga.getID());
				startActivityForResult(intent, RAGA_FROM_FLASH_CARDS);
			}
		});
		Button mSettingsButton = (Button) findViewById(R.id.flashcard_settings_button);
		mSettingsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				FlashCardsSettingsDialog dialog = new FlashCardsSettingsDialog(
						mInstance);
				dialog.show();
			}
		});
		ImageButton mNextButton = (ImageButton) findViewById(R.id.flashcard_next_button);
		mNextButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				nextQuestion();
			}
		});
		mPromptText = (TextView) findViewById(R.id.flashcard_prompt_text);
		mQuestionText = (TextView) findViewById(R.id.flashcard_question_text);
		mQuestionText.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				playAudio();
			}
		});
		mAnswerText = (TextView) findViewById(R.id.flashcard_answer_text);
		mContext = getApplicationContext();
		mDB = DBAdapter.getInstance(mContext);
		mSoundManager = SoundManager.getInstance(mContext);
		mRandom = new Random();
	}

	@Override
	protected void onResume() {
		super.onResume();
		nextQuestion();
	}

	private void nextQuestion() {
		SharedPreferences values = PreferenceManager
				.getDefaultSharedPreferences(mContext);
		boolean validPrompts[] = { values.getBoolean("RBA", true),
				values.getBoolean("RBS", true), values.getBoolean("SBR", true) };
		do {
			mPromptType = mRandom.nextInt(3);
		} while (!validPrompts[mPromptType]);
		mPromptText.setText(PROMPTS[mPromptType]);
		mRaga = mDB.getRaga(mRandom.nextInt(NUM_RAGAS) + 1);
		if (mRaga == null) {
			Log.v("FLASHCARDS", "NULL");
			return;
		}

		switch (mPromptType) {
		case 0:
			playAudio();
			mQuestionText.setText("Click here to play the audio again.");
			mQuestionText.setEnabled(true);
			break;
		case 1:
			mQuestionText.setText(mRaga.printArohana());
			mQuestionText.setEnabled(false);
			break;
		case 2:
			mQuestionText.setText(mRaga.getName());
			mQuestionText.setEnabled(false);
			break;
		}
		mAnswerText.setText("");
		mGoToRagaButton.setEnabled(false);
	}

	private void showAnswer() {
		switch (mPromptType) {
		case 0:
			mAnswerText.setText(mRaga.getName());
		case 1:
			mAnswerText.setText(mRaga.getName());
			break;
		case 2:
			mAnswerText.setText(mRaga.printArohana());
			break;
		}
		mGoToRagaButton.setEnabled(true);
	}

	private void playAudio() {
		mSoundManager.playScale(mRaga.printArohana());
	}
}
